#include "SCamera.h"

void	SCamera::SetDevice( LPDIRECT3DDEVICE9 pd3dDevice )
{
	m_pd3dDevice = pd3dDevice;
}

SCamera::SCamera(void)
{
	xAngle = 0.0f;
	yAngle = 0.0f;
	m_pFrustum = NULL;
	InIt();
	
}

bool	SCamera::InIt()
{
	vPosion			= D3DXVECTOR3( 0.0f, 10.0f, 0.0f );
	vLookVector		= D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
	vUpVecTor		= D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
	vRightVector	= D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
	IsMove = false;
	m_bMakeFrustum = true;
	SAFE_NEW( m_pFrustum, SFrustum );
	m_bFrustumRender = true;
	deltatpos.x	= 0.0f;
	deltatpos.y = 0.0f;
	m_Input.Init();
	
	return true;
}

bool SCamera::CreateWorldMatrix()
{
	D3DXMatrixIdentity( &matWorld );	
	m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
	return true;
}

bool SCamera::CreateViewMatrix( D3DXVECTOR3 vPos, D3DXVECTOR3 vTarget )
{
	D3DXVECTOR3	vUpVec( 0.0f, 1.0f, 0.0f );
	vPosion = vPos;
	D3DXMatrixLookAtLH( &matView, &vPos, &vTarget, &vUpVec );
	m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

	vRightVector.x	= matView._11;
	vRightVector.y	= matView._21;
	vRightVector.z	= matView._31;

	vUpVecTor.x		= matView._12;
	vUpVecTor.y		= matView._22;
	vUpVecTor.z		= matView._32;

	vLookVector.x	= matView._13;
	vLookVector.y	= matView._23;
	vLookVector.z	= matView._33;
	return true;
}

bool SCamera::CreateProjecTionMatrix()
{
	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.333333f, 1.0f, 10000.0f );
	m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
	return true;
}

bool SCamera::SetVIewMatrix()
{
	matView._11 = vRightVector.x;
	matView._12 = vRightVector.y;
	matView._13 = vRightVector.z;

	matView._21 = vUpVecTor.x;
	matView._22 = vUpVecTor.y;
	matView._23 = vUpVecTor.z;

	matView._31 = vLookVector.x;
	matView._32 = vLookVector.y;
	matView._33 = vLookVector.z;

	matView._41 = vPosion.x;
	matView._42 = vPosion.y;
	matView._43 = vPosion.z;

	D3DXMatrixInverse( &matView, NULL, &matView );
	m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
	return false;
}

bool SCamera::Frame()
{
	float delta = 0.5f;
	
	m_Input.Frame();
	m_Time.Frame();	

	if( m_Input.GetKey('W') )
	{
		ZoomUpMove( delta );
	}
	else if( m_Input.GetKey('S') )
	{
		ZoomDownMove( delta );
	}
	if( m_Input.GetKey('A') )
	{
		UpMove( delta );
	}
	else if( m_Input.GetKey('D') )
	{
		DownMove( delta );
	}
	if( m_Input.GetKey('Q') )
	{
		LeftMove( delta );
	}
	else if( m_Input.GetKey('E') )
	{
		RightMove( delta );
	}
	if( m_Input.GetKey( VK_RBUTTON ) )
	{
		SetVIewMatrix();		
		deltatpos = m_Input.GetCurMovePos();
		if( deltatpos.x != 0 || deltatpos.y !=0 )
		{
			Rotate( deltatpos.y * m_Time.m_fSecPerFrame * 0.5f,
			    	deltatpos.x * m_Time.m_fSecPerFrame * 0.5f );

		}
	}

	/*if( IsMove == true )
	{
		SetVIewMatrix();
		IsMove = false;
	}*/
	if( m_bMakeFrustum )
	{
		MakeFrustum( this->matView	, this->matProj );
		m_pFrustum->m_vNearCenter = vPosion;
	}
	return true;	
}

bool SCamera::MakeFrustum( D3DXMATRIX matView,D3DXMATRIX matProj )
{
	m_pFrustum->SetMatrix( matView, matProj );
	m_pFrustum->Frame();
	return true;
}

SFrustum* SCamera::GetFrustum()
{
	if( m_pFrustum == NULL )
	{
		SAFE_NEW( m_pFrustum, SFrustum );
	}
	return m_pFrustum;

}
void SCamera::UpMove( float delta )
{
	vPosion -= vUpVecTor * delta;
	IsMove = true;
	SetVIewMatrix();
}

void SCamera::DownMove( float delta )
{
	vPosion += vUpVecTor * delta;
	IsMove = true;
	SetVIewMatrix();
}

void SCamera::RightMove( float delta )
{
	vPosion -= vRightVector * delta;
	IsMove = true;
	SetVIewMatrix();
}

void SCamera::LeftMove( float delta )
{
	vPosion += vRightVector * delta; 
	IsMove = true;
	SetVIewMatrix();
}

void SCamera::ZoomUpMove( float delta )
{
	vPosion += vLookVector * delta;
	IsMove = true;
	SetVIewMatrix();
}

void SCamera::ZoomDownMove( float delta )
{
	vPosion -= vLookVector * delta; 
	IsMove = true;
	SetVIewMatrix();
}

void SCamera::Rotate( float deltaX,float deltaY )
{
	xAngle	+=	deltaX;
	yAngle	+=	deltaY;

	D3DXQuaternionRotationYawPitchRoll( &qRotate, yAngle, xAngle, 0.0f );
	D3DXMatrixAffineTransformation( &matView, 1.0f, NULL, &qRotate, &vPosion );

	vPosion.x = matView._41;
	vPosion.y = matView._42;
	vPosion.z = matView._43;

	
	D3DXMatrixInverse( &matView, NULL, &matView );	

	vRightVector.x	= matView._11;
	vRightVector.y	= matView._21;
	vRightVector.z	= matView._31;

	vUpVecTor.x		= matView._12;
	vUpVecTor.y		= matView._22;
	vUpVecTor.z		= matView._32;

	vLookVector.x	= matView._13;
	vLookVector.y	= matView._23;
	vLookVector.z	= matView._33;

	m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
	IsMove = false;
}

bool SCamera::Release()
{
	SAFE_DEL( m_pFrustum );
	return true; 
}

bool SCamera::Input()
{
	return true;
}

bool SCamera::Render()
{
	if( m_bFrustumRender )
	{
		m_pFrustum->Render(this->m_pd3dDevice);
	}
	return true;
}
void SCamera::DrawFrustum()
{
	//m_pFrustum->Render();
}




SCamera::~SCamera(void)
{
	
}



